/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.server.data.img;

import hope.platform.data.dao.RoleRepository;
import hope.platform.data.domain.Role;
import hope.platform.data.domain.entity.RoleAuthority;
import hope.platform.data.server.data.header.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/17 9:51
 */
@Service
public class RoleServiceImg implements RoleService {

    @Autowired
    private RoleRepository repository;

    @Override
    public Role getByRoleName(String roleName) {
        return repository.findByRoleName(roleName);
    }

    @Override
    public Role addRole(Role role) {
        role.setGmtModified(new Date());
        role.setGmtCreate( new Date());
        return repository.save(role);
    }

    @Override
    public Role updateRole(Role role) {
        Role tmp = repository.findByRoleName(role.getRoleName());
        for (Map.Entry<String, RoleAuthority> entry : role.getAuthorities().entrySet()) {
            tmp.getAuthorities().put(entry.getKey(), entry.getValue());
        }
        tmp.setGmtModified(new Date());
        return repository.save(tmp);
    }

    @Override
    public List<Role> getAll() {
        return repository.findAll();
    }
}
